import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_base/public/components/layout_widgets.dart';

import 'items/item.dart';
import 'logic.dart';
import 'state.dart';

class ConversationWidget extends StatelessWidget {
  final ConversationLogic logic = Get.put(ConversationLogic());
  final ConversationState state = Get.find<ConversationLogic>().state;
  final Function(String, String, bool) itemClick;

  ConversationWidget({required this.itemClick});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ConversationLogic>(
        init: logic,
        builder: (controller) {
          return LoadingWidget(
            status: controller.state.loadingStatus,
            child: Container(
              child: ListView.builder(
                itemCount: controller.state.conversationList.length,
                itemBuilder: (context, index) {
                  if (controller.state.hasMore &&
                      index >= controller.state.conversationList.length - 1) {
                    controller.loadMore();
                  }
                  return ConversationItem(
                    key: ValueKey(
                        state.conversationList[index]?.conversationID ?? ''),
                    itemData: state.conversationList[index]!,
                    conversationClick: itemClick,
                    deleteItem: (data) => logic
                        .findLogic<ConversationLogic>()
                        .deleteConversation(data),
                  );
                },
              ),
            ),
          );
        });
  }
}
